package com.xin.data.simbapesa.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.Html;
import android.view.KeyEvent;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;

import com.xin.data.simbapesa.R;
import com.xin.data.simbapesa.SimbaApp;
import com.xin.data.simbapesa.api.PU;
import com.xin.data.simbapesa.base.BaseActivity;
import com.xin.data.simbapesa.databinding.SimbaTimeActivityBinding;
import com.xin.data.simbapesa.utils.LogUtil;
import com.xin.data.simbapesa.utils.ResUtils;
import com.xin.data.simbapesa.utils.ViewUtils;

public class SimbaTimeActivity extends BaseActivity<SimbaTimeActivityBinding> {



    private final int TIME_WHAT = 1;

    private int timeCout = 15;


    @Override
    protected SimbaTimeActivityBinding initViewBinding(LayoutInflater inflater) {
        return SimbaTimeActivityBinding.inflate(inflater);
    }

    @Override
    protected void initView() {

        binding.tvSubTitle.setText(Html.fromHtml(ResUtils.s(R.string.time_subtitle)));
        binding.tvSubTitle1.setText(Html.fromHtml(ResUtils.s(R.string.time_subtitle1)));
        binding.tvSubTitle2.setText(Html.fromHtml(ResUtils.s(R.string.time_subtitle2)));
        binding.tvSubTitle3.setText(Html.fromHtml(ResUtils.s(R.string.time_subtitle3)));


        binding.tvCs.setOnClickListener(v -> {
            ViewUtils.viewScale(v);
            startActivity(new Intent(getActivity(), SimbaCustomerServiceActivity.class));
        });
        String str = timeCout + " S";
        binding.tvTitle.setText(str);


    }

    @Override
    protected void initData() {
        UP_TYPE1 = PU.P104;
        UP_TYPE2 = PU.P105;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mHandler.sendEmptyMessageDelayed(TIME_WHAT, 1000);
        userProcess(new UserProcessInterface() {
            @Override
            public void onRequestResult() {
                boolean isCheckSteps = checkSteps();
                LogUtil.e("me", "isCheckSteps->" + isCheckSteps);
                if (isCheckSteps) {
                    mHandler.removeMessages(TIME_WHAT);
                }
            }
        }, true);
    }

    @SuppressLint("HandlerLeak")
    final
    Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == TIME_WHAT) {
                mHandler.removeMessages(TIME_WHAT);
                timeCout = timeCout - 1;
                if (timeCout == 10) {
                    binding.tvBox1.setImageResource(R.mipmap.time_box2);
                    binding.tvBox2.setImageResource(R.mipmap.time_box1);
                    binding.tvBox3.setImageResource(R.mipmap.time_box1);
                } else if (timeCout == 5) {
                    binding.tvBox1.setImageResource(R.mipmap.time_box2);
                    binding.tvBox2.setImageResource(R.mipmap.time_box2);
                    binding.tvBox3.setImageResource(R.mipmap.time_box1);
                } else if (timeCout == 0) {
                    binding.tvBox1.setImageResource(R.mipmap.time_box2);
                    binding.tvBox2.setImageResource(R.mipmap.time_box2);
                    binding.tvBox3.setImageResource(R.mipmap.time_box2);
                }
                if (timeCout <= 0) {
                    userProcess(() -> {
                        boolean isCheckSteps = checkSteps(getActivity());
                        if (!isCheckSteps) {
                            SimbaApp.getInstance().clearActivity();
                            Intent intent = new Intent(getActivity(), SimbaHomeActivity.class);
                            startActivity(intent);
                        }
                        finish();
                    }, true);

                } else {
                    String str = timeCout + " S";
                    binding.tvTitle.setText(str);
                    mHandler.sendEmptyMessageDelayed(TIME_WHAT, 1000);
                }
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHandler != null) {
            mHandler.removeMessages(TIME_WHAT);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }



}
